package io.bdmc.modules.bss.controller;

import java.time.LocalDateTime;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.bdmc.common.annotation.SysLogger;
import io.bdmc.common.utils.MyMap;
import io.bdmc.common.utils.MyPage;
import io.bdmc.common.utils.R;
import io.bdmc.common.validator.ValidatorUtils;
import io.bdmc.common.validator.group.AddGroup;
import io.bdmc.common.validator.group.UpdateGroup;
import io.bdmc.modules.bss.entity.SmkMuser;
import io.bdmc.modules.bss.model.MuserParam;
import io.bdmc.modules.bss.service.ISmkMuserService;
import io.bdmc.modules.sys.controller.AbstractController;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HuangLu
 * @since 2019-02-15
 */
@RestController
@RequestMapping("/bss/muser")
public class SmkMuserController extends AbstractController{
    @Autowired
    private ISmkMuserService _muserSvs;


    @RequestMapping("/list")
	@RequiresPermissions("bss:muser:list")
	public R list(MuserParam params){
        MyPage<SmkMuser> myPage = new MyPage<SmkMuser>(params.getCurrent(), params.getSize());
        MyPage<SmkMuser> map = _muserSvs.mySelectPage(myPage, params);
        MyMap<String, Object> s = MyMap.ok(map.getRecords(), map.getTotal());
		return R.ok().putData(s);

	}


    /**
	 * 新增维保人员
	 */
	@SysLogger("新增维保人员")
	@RequestMapping("/save")
	@RequiresPermissions("bss:muser:save")
	public R save(@RequestBody SmkMuser muser){
		ValidatorUtils.validateEntity(muser, AddGroup.class);
		String pwd =new Sha256Hash(muser.getPassword(), muser.getMobile()).toHex() ;	
		muser.setPassword(pwd);
        muser.setCreateTime(LocalDateTime.now());
		Boolean b = muser.insert();	
		return R.trueOfFalse(b);
    }

    /**
	 * 修改维保人员
	 */
	@SysLogger("修改维保人员")
	@RequestMapping("/update")
	@RequiresPermissions("bss:muser:update")
	public R update(@RequestBody SmkMuser shop){
		ValidatorUtils.validateEntity(shop, UpdateGroup.class);
		Boolean b = shop.updateById();
		return R.trueOfFalse(b);
    }

    
    /**
	 * 删除维保人员
	 */
    @SysLogger("删除维保人员")
	@RequestMapping("/delete")
	@RequiresPermissions("bss:muser:delete")
	public R delete(@RequestBody long muserId) {
		if(muserId>0)
		_muserSvs.removeById(muserId);
		return R.ok();
    }
    
    /**
	 * 重置密码
	 */
    @SysLogger("重置维保人员密码")
	@RequestMapping("/resetpwd")
	@RequiresPermissions("bss:muser:resetpwd")
	public R resetPwd(@RequestBody long muserId) {
		 SmkMuser u = _muserSvs.getById(muserId);
		if(u==null){
			return R.error(-1,"参数错误");
		}
		String pwd =new Sha256Hash("123456", u.getMobile()).toHex() ;	
		u.setPassword(pwd);
		Boolean re = u.updateById();
		return R.trueOfFalse(re);
    }
}

